// Range v3 library
//
//  Copyright Eric Niebler 2014
//
//  Use, modification and distribution is subject to the
//  Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ericniebler/range-v3
//
//  Copyright 2005 - 2007 Adobe Systems Incorporated
//  Distributed under the MIT License(see accompanying file LICENSE_1_0_0.txt
//  or a copy at http://stlab.adobe.com/licenses.html)

//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include <nanorange/algorithm/remove_if.hpp>
#include <iostream>
#include <memory>
#include <utility>
#include <functional>
#include "../catch.hpp"
#include "../test_iterators.hpp"
#include "../test_utils.hpp"

namespace stl2 = nano;

namespace {

template <typename T>
T& as_lvalue(T&& t)
{
    return t;
}

template<class Iter, class Sent = Iter>
void
test_iter() {
	int ia[] = {0, 1, 2, 3, 4, 2, 3, 4, 2};
	constexpr unsigned sa = stl2::size(ia);
	using namespace std::placeholders;
	Iter r = stl2::remove_if(Iter(ia), Sent(ia + sa), std::bind(std::equal_to<int>(), _1, 2));
	CHECK(base(r) == ia + sa - 3);
	CHECK(ia[0] == 0);
	CHECK(ia[1] == 1);
	CHECK(ia[2] == 3);
	CHECK(ia[3] == 4);
	CHECK(ia[4] == 3);
	CHECK(ia[5] == 4);
}

template<class Iter, class Sent = Iter>
void
test_range() {
	int ia[] = {0, 1, 2, 3, 4, 2, 3, 4, 2};
	constexpr unsigned sa = stl2::size(ia);
	using namespace std::placeholders;
	Iter r = stl2::remove_if(::as_lvalue(stl2::subrange(Iter(ia), Sent(ia + sa))),
							 std::bind(std::equal_to<int>(), _1, 2));
	CHECK(base(r) == ia + sa - 3);
	CHECK(ia[0] == 0);
	CHECK(ia[1] == 1);
	CHECK(ia[2] == 3);
	CHECK(ia[3] == 4);
	CHECK(ia[4] == 3);
	CHECK(ia[5] == 4);
}

struct pred {
	bool operator()(const std::unique_ptr<int> &i) { return *i == 2; }
};

template<class Iter, class Sent = Iter>
void
test_iter_rvalue() {
	constexpr unsigned sa = 9;
	std::unique_ptr<int> ia[sa];
	ia[0].reset(new int(0));
	ia[1].reset(new int(1));
	ia[2].reset(new int(2));
	ia[3].reset(new int(3));
	ia[4].reset(new int(4));
	ia[5].reset(new int(2));
	ia[6].reset(new int(3));
	ia[7].reset(new int(4));
	ia[8].reset(new int(2));
	Iter r = stl2::remove_if(Iter(ia), Sent(ia + sa), pred());
	CHECK(base(r) == ia + sa - 3);
	CHECK(*ia[0] == 0);
	CHECK(*ia[1] == 1);
	CHECK(*ia[2] == 3);
	CHECK(*ia[3] == 4);
	CHECK(*ia[4] == 3);
	CHECK(*ia[5] == 4);
}

template<class Iter, class Sent = Iter>
void
test_range_rvalue() {
	constexpr unsigned sa = 9;
	std::unique_ptr<int> ia[sa];
	ia[0].reset(new int(0));
	ia[1].reset(new int(1));
	ia[2].reset(new int(2));
	ia[3].reset(new int(3));
	ia[4].reset(new int(4));
	ia[5].reset(new int(2));
	ia[6].reset(new int(3));
	ia[7].reset(new int(4));
	ia[8].reset(new int(2));
	Iter r = stl2::remove_if(::as_lvalue(stl2::subrange(Iter(ia), Sent(ia + sa))), pred());
	CHECK(base(r) == ia + sa - 3);
	CHECK(*ia[0] == 0);
	CHECK(*ia[1] == 1);
	CHECK(*ia[2] == 3);
	CHECK(*ia[3] == 4);
	CHECK(*ia[4] == 3);
	CHECK(*ia[5] == 4);
}

struct S {
	int i;
};

}

TEST_CASE("alg.remove_if")
{
	test_iter<forward_iterator<int*> >();
	test_iter<bidirectional_iterator<int*> >();
	test_iter<random_access_iterator<int*> >();
	test_iter<int*>();
	test_iter<forward_iterator<int*>, sentinel<int*>>();
	test_iter<bidirectional_iterator<int*>, sentinel<int*>>();
	test_iter<random_access_iterator<int*>, sentinel<int*>>();

	test_range<forward_iterator<int*> >();
	test_range<bidirectional_iterator<int*> >();
	test_range<random_access_iterator<int*> >();
	test_range<int*>();
	test_range<forward_iterator<int*>, sentinel<int*>>();
	test_range<bidirectional_iterator<int*>, sentinel<int*>>();
	test_range<random_access_iterator<int*>, sentinel<int*>>();

	test_iter_rvalue<forward_iterator<std::unique_ptr<int>*> >();
	test_iter_rvalue<bidirectional_iterator<std::unique_ptr<int>*> >();
	test_iter_rvalue<random_access_iterator<std::unique_ptr<int>*> >();
	test_iter_rvalue<std::unique_ptr<int>*>();
	test_iter_rvalue<forward_iterator<std::unique_ptr<int>*>, sentinel<std::unique_ptr<int>*>>();
	test_iter_rvalue<bidirectional_iterator<std::unique_ptr<int>*>, sentinel<std::unique_ptr<int>*>>();
	test_iter_rvalue<random_access_iterator<std::unique_ptr<int>*>, sentinel<std::unique_ptr<int>*>>();

	test_range_rvalue<forward_iterator<std::unique_ptr<int>*> >();
	test_range_rvalue<bidirectional_iterator<std::unique_ptr<int>*> >();
	test_range_rvalue<random_access_iterator<std::unique_ptr<int>*> >();
	test_range_rvalue<std::unique_ptr<int>*>();
	test_range_rvalue<forward_iterator<std::unique_ptr<int>*>, sentinel<std::unique_ptr<int>*>>();
	test_range_rvalue<bidirectional_iterator<std::unique_ptr<int>*>, sentinel<std::unique_ptr<int>*>>();
	test_range_rvalue<random_access_iterator<std::unique_ptr<int>*>, sentinel<std::unique_ptr<int>*>>();

	{
		// Check projection
		S ia[] = {S{0}, S{1}, S{2}, S{3}, S{4}, S{2}, S{3}, S{4}, S{2}};
		constexpr unsigned sa = stl2::size(ia);
		using namespace std::placeholders;
        // FIXME (MSVC): The std::bind call started failing with VS 16.5, so we'll work around it
        // The problem seems to occur in evaluating the default_initializable concept
        // I'm not really sure what's going on, but as long as it's only with std::bind I don't care that much
#if defined(_MSC_VER) && _MSC_VER >= 1925
        S* r = stl2::remove_if(ia, [](int i) { return i == 2; }, &S::i);
#else
		S* r = stl2::remove_if(ia, std::bind(std::equal_to<int>(), _1, 2), &S::i);
#endif
		CHECK(r == ia + sa-3);
		CHECK(ia[0].i == 0);
		CHECK(ia[1].i == 1);
		CHECK(ia[2].i == 3);
		CHECK(ia[3].i == 4);
		CHECK(ia[4].i == 3);
		CHECK(ia[5].i == 4);
	}

	{
		// Check rvalue range
		S ia[] = {S{0}, S{1}, S{2}, S{3}, S{4}, S{2}, S{3}, S{4}, S{2}};
		constexpr unsigned sa = stl2::size(ia);
		using namespace std::placeholders;
		// FIXME (MSVC): As above
#if defined(_MSC_VER) && _MSC_VER >= 1925
		auto r = stl2::remove_if(stl2::subrange(ia), [](int i){ return i == 2; }, &S::i);
#else
		auto r = stl2::remove_if(stl2::subrange(ia), std::bind(std::equal_to<int>(), _1, 2), &S::i);
#endif
		CHECK(r == ia + sa-3);
		CHECK(ia[0].i == 0);
		CHECK(ia[1].i == 1);
		CHECK(ia[2].i == 3);
		CHECK(ia[3].i == 4);
		CHECK(ia[4].i == 3);
		CHECK(ia[5].i == 4);
	}
}
